package net.jeeshop.web.controller.manage.system;

import net.jeeshop.core.ManageContainer;
import net.jeeshop.core.Services;
import net.jeeshop.core.common.dao.page.PagerModel;
import net.jeeshop.core.common.exception.NotThisMethod;
import net.jeeshop.services.manage.system.bean.Role;
import net.jeeshop.services.manage.system.bean.User;
import net.jeeshop.services.manage.system.impl.RoleService;
import net.jeeshop.web.controller.BaseController;
import net.jeeshop.web.util.LoginUserHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 角色action
 *
 * @author huangf
 * @author jeeshopfans
 */
@Controller
@RequestMapping("/manage/role")
public class RoleAction extends BaseController<Role> {
    private static final Logger logger = LoggerFactory.getLogger(RoleAction.class);
    @Resource
    private RoleService roleService;

    public RoleAction() {
        super.page_toList = "/manage/system/role/roleList";
        super.page_toEdit = "/manage/system/role/editRole";
        super.page_toAdd = "/manage/system/role/addRole";
    }

    @Override
    @RequestMapping(value = "deletes", method = RequestMethod.POST)
    public String deletes(HttpServletRequest request, String[] ids, @ModelAttribute("e") Role e, RedirectAttributes flushAttrs) throws Exception {
        throw new NotThisMethod(ManageContainer.not_this_method);
    }

    @Override
    public Services<Role> getService() {
        return this.roleService;
    }

    @Override
    public void insertAfter(Role e) {
        e.clear();
    }

    @Override
    protected void selectListAfter(PagerModel pager) {
        pager.setPagerUrl("selectList");
    }

    /**
     * 只能是admin才具有编辑其他用户权限的功能
     */
    @Override
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(HttpServletRequest request, @ModelAttribute("e") Role role, RedirectAttributes flushAttrs) throws Exception {
        User user = LoginUserHolder.getLoginUser();
        if (!user.getUsername().equals("admin")) {
            throw new NullPointerException(ManageContainer.RoleAction_update_error);
        }
        return super.update(request, role, flushAttrs);
    }
}
